package com.tangyee.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.tangyee.MainActivity;
import com.tangyee.MainActivity;
import com.tangyee.home.activity.db.UsersInfo;
import com.tangyee.utils.SessionHelper;
import com.tangyee.utils.SessionUtil;

import org.litepal.LitePal;

import java.util.List;

public class BasicActivity extends AppCompatActivity implements SessionHelper {
    private List<UsersInfo> userdinfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        localLogin();
        userdinfo = LitePal.findAll(UsersInfo.class);
        for(int i=0;i<userdinfo.size();i++){
            if(userdinfo.get(i).getStatus()==1){
                Log.d(TAG,"本地的sessionid为"+userdinfo.get(i).getSessionid());
                Intent intent = new Intent(this, MainActivity.class);
                Log.d(TAG,"即将发送的用户名为："+userdinfo.get(i).getUsername());
                intent.putExtra(USERNAME,userdinfo.get(i).getUsername());
                Log.d(TAG,"即将发送的密码是"+userdinfo.get(i).getPassword());
                intent.putExtra(PASSWORD,userdinfo.get(i).getPassword());
                intent.putExtra("preActivity","BasicActivity.class");
//                Bundle bundle = new Bundle();
//                bundle.putString(USERNAME,userdinfo.get(i).getUsername());
//                bundle.putString(PASSWORD,userdinfo.get(i).getPassword());
//                intent.putExtra("DATA",bundle);
                startActivity(intent);
                finish();
                return;
            }
        }
        Intent intent = new Intent(this, Login.class);
        startActivity(intent);
        finish();
    }

    public void localLogin() {
        Log.d(TAG,"执行了localLogin");
        List<UsersInfo> list =null;
        list= LitePal.findAll(UsersInfo.class);
        Log.d(TAG,list.get(0).getSessionid());
        if (list!=null) {
            //本地有时就直接登录，然后在刷新sessionid,
            for (UsersInfo usr : list) {
                if (usr.getStatus() == 1) {
                    Intent intent = new Intent(I_NEED_SESSIONID_BY_BROAD);
                    intent.putExtra(USERNAME, usr.getUsername());
                    intent.putExtra(PASSWORD, usr.getPassword());
                    sendBroadcast(intent);
                    Log.d(TAG, "发送了本地查询广播并且sessionid是" + usr.getSessionid());
                    gotoMainActivity();
                }
            }
        }else {
            gotoLoginActivity();
        }
    }

    public void gotoMainActivity() {
        Log.d(TAG,"执行了GotoMainActivity");
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
        return;
    }

    public void gotoLoginActivity(){
        Log.d(TAG,"执行了GotoLoginActivity");
        Intent intent = new Intent(this, Login.class);
        startActivity(intent);
        finish();
    }
    @Override
    public void registerSessionReceiver() {

    }

    @Override
    public void unregisterSessionReceiver() {

    }

    private static final String TAG = "BasicActivity";
}
